/*
* Copyright (C) 2010-2012 Magnus Skjegstad
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
* 
* The latest version of this library can be downloaded from http://mist-pubsub.googlecode.com
* 
*/
package com.skjegstad.mist2.demos;

import com.skjegstad.mist2.IMistSubscriptionListener;
import com.skjegstad.mist2.MistCoreManager;
import com.skjegstad.mist2.datatypes.DataElement;
import com.skjegstad.mist2.exceptions.MistException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 *
 * @author Magnus Skjegstad
 */
public class TestDataConsumer {

    public static void main(String[] argv) throws UnknownHostException, InterruptedException, MistException, IOException {
        MistCoreManager mist = new MistCoreManager();
        //mist.enableBroadcast(InetAddress.getByName("255.255.255.255"), 5579);
        mist.enableMulticast(InetAddress.getByName("224.0.0.1"), 5579);
        mist.setDebug(true);
        
        final BlockingQueue<DataElement> queue = new LinkedBlockingQueue();

        mist.subscribe("no.ffi.stream.test", new IMistSubscriptionListener() {
            public void deliverMessage(DataElement message) {
                queue.add(message);
            }
        });

        mist.start();

        long recvSoFar = 0;
        long start = System.currentTimeMillis();
        while (true && !Thread.interrupted()) {
            DataElement message = queue.take();
            recvSoFar = recvSoFar + message.getPayloadLen() ;
            long timeSpent = System.currentTimeMillis() - start;
            double bw = recvSoFar / (double)(timeSpent / 1000.0);
            System.err.println("Got message #" + message.getSeqNr() + " (" + message.getPayloadLen()+ " bytes). Bw: " + bw);            
        }

        mist.stop();

    }
}
